package com.xzssmk.leisure.x.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzssmk.leisure.x.mapper.*;
import com.xzssmk.leisure.x.po.CardInventory;
import com.xzssmk.leisure.x.po.PeriodLargessExt;
import com.xzssmk.leisure.x.po.PeriodSpotExt;
import com.xzssmk.leisure.x.service.ReportStatisticsService;
import com.xzssmk.leisure.x.util.DataTables;
import com.xzssmk.leisure.x.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ReportStatisticsServiceImpl implements ReportStatisticsService {

    @Autowired
    CardInventoryMapper cardInventoryMapper;

    @Autowired
    OrderConsumeMapper orderConsumeMapper;

    @Autowired
    PeriodLargessMapper periodLargessMapper;

    @Autowired
    CommissionMapper commissionMapper;

    @Autowired
    PeriodSpotMapper periodSpotMapper;

    @Autowired
    IssuePeriodMapper issuePeriodMapper;

    @Autowired
    ReviewScanMapper reviewScanMapper;

    @Override
    public List<SaleCardReportVo> getSaleCardReportList(Integer issuePeriodId) {
        return orderConsumeMapper.getSaleCardReportList(issuePeriodId);
    }

    @Override
    public DataTables getPageList1(DataTables dataTables) {
        PageHelper.startPage(dataTables.getStart(), dataTables.getLength()); // 核心分页代码
        PageHelper.orderBy("inventory_id desc");

        PageInfo<CardInventory> pageInfo = new PageInfo<CardInventory>(
            cardInventoryMapper.getPageList1(dataTables.getSearch(), dataTables.getSubSQL()));
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<>());

        return dataTables;
    }

    @Override
    public DataTables bbPageList(DataTables dataTables, String issuePeriodId, String storeLargessId) {
        PageHelper.startPage(dataTables.getStart(), dataTables.getLength()); // 核心分页代码
        PageHelper.orderBy("modify_time desc");

        List<PeriodLargessExt> periodLargessExts = periodLargessMapper.bbPageList(issuePeriodId, storeLargessId);
        PageInfo<PeriodLargessExt> pageInfo = new PageInfo<PeriodLargessExt>(periodLargessExts);
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<>());
        return dataTables;
    }

    @Override
    public DataTables proportionPageList(DataTables dataTables, String issuePeriodId) {
        PageHelper.startPage(dataTables.getStart(), dataTables.getLength()); // 核心分页代码
//        PageHelper.orderBy("modify_time desc");

        List<CommissionViewVo> commissionViewVos = commissionMapper.proportionPageList(issuePeriodId);
        PageInfo<CommissionViewVo> pageInfo = new PageInfo<CommissionViewVo>(commissionViewVos);
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<>());
        return dataTables;
    }

    @Override
    public DataTables getSCardPageList(DataTables dataTables, String issuePeriodId,String splitPattern) {
        PageHelper.startPage(dataTables.getStart(), dataTables.getLength()); // 核心分页代码
//        PageHelper.orderBy("modify_time desc");

        List<PeriodSpotExt> periodSpotExts = periodSpotMapper.getSCardPageList(issuePeriodId,splitPattern);
        PageInfo<PeriodSpotExt> pageInfo = new PageInfo<PeriodSpotExt>(periodSpotExts);
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<>());
        return dataTables;
    }

    @Override
    public DataTables bbgetOrderList(DataTables dataTables, String issuePeriodId, String purchaseMethod) {
        PageHelper.startPage(dataTables.getStart(), dataTables.getLength()); // 核心分页代码
//        PageHelper.orderBy("modify_time desc");

        List<OrderReportVo> periodSpotExts = orderConsumeMapper.bbgetOrderList(issuePeriodId, purchaseMethod);
        PageInfo<OrderReportVo> pageInfo = new PageInfo<OrderReportVo>(periodSpotExts);
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<>());
        return dataTables;
    }

    @Override
    public DataTables bbProportionCleanUpPageList(DataTables dataTables, String issuePeriodId, String scenicSpotId) {

        PageHelper.startPage(dataTables.getStart(), dataTables.getLength()); // 核心分页代码
//        PageHelper.orderBy("modify_time desc");

        List<BBProportion> periodSpotExts = issuePeriodMapper.bbProportionCleanUpPageList(issuePeriodId, scenicSpotId);
        PageInfo<BBProportion> pageInfo = new PageInfo<BBProportion>(periodSpotExts);
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<>());
        return dataTables;
    }

    @Override
    public DataTables swipeCardList(DataTables dataTables, String issuePeriodId, String scenicSpotId) {
        PageHelper.startPage(dataTables.getStart(), dataTables.getLength()); // 核心分页代码
//        PageHelper.orderBy("modify_time desc");

        List<SwipeCardVo> periodSpotExts = reviewScanMapper.swipeCardList(issuePeriodId, scenicSpotId);
        PageInfo<SwipeCardVo> pageInfo = new PageInfo<SwipeCardVo>(periodSpotExts);
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<>());
        return dataTables;
    }

    @Override
    public DataTables cardUseHistory(DataTables dataTables, String cardNumber) {
        PageHelper.startPage(dataTables.getStart(), dataTables.getLength()); // 核心分页代码
//        PageHelper.orderBy("modify_time desc");

        List<CardUseHistory> cardUseHistories = reviewScanMapper.cardUseHistory(cardNumber);
        PageInfo<CardUseHistory> pageInfo = new PageInfo<>(cardUseHistories);
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<>());
        return dataTables;
    }

    @Override
    public DataTables bbSaleCardCountList(DataTables dataTables, String issuePeriodId) {
        PageHelper.startPage(dataTables.getStart(), dataTables.getLength()); // 核心分页代码
//        PageHelper.orderBy("modify_time desc");

        List<SaleCardCountList> saleCardCountLists = issuePeriodMapper.bbSaleCardCountList(issuePeriodId);
        PageInfo<SaleCardCountList> pageInfo = new PageInfo<>(saleCardCountLists);
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<>());
        return dataTables;
    }

    @Override
    public DataTables bbIssuePeriodInComeList(DataTables dataTables, String issuePeriodId) {
        PageHelper.startPage(dataTables.getStart(), dataTables.getLength()); // 核心分页代码
//        PageHelper.orderBy("modify_time desc");

        List<IssuePeriodInCome> issuePeriodInComes = issuePeriodMapper.bbIssuePeriodInComeList(issuePeriodId);
        PageInfo<IssuePeriodInCome> pageInfo = new PageInfo<>(issuePeriodInComes);
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<>());
        return dataTables;
    }
}
